package com.xintonghua.hongyi.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioGroup;

import com.xintonghua.base.ui.BaseActivity;
import com.xintonghua.hongyi.R;
import com.xintonghua.hongyi.ui.fragment.CultureFragment;
import com.xintonghua.hongyi.ui.fragment.HomeFragment;
import com.xintonghua.hongyi.ui.fragment.MyFragment;
import com.xintonghua.hongyi.ui.fragment.PriceFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rg_tab)
    RadioGroup rgTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if (savedInstanceState == null) {
            HomeFragment homeFragment = new HomeFragment();
            CultureFragment cultureFragment = new CultureFragment();
            PriceFragment priceFragment = new PriceFragment();
            MyFragment myFragment = new MyFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fl_wrap, myFragment, getString(R.string.me_page)).hide(myFragment)
                    .add(R.id.fl_wrap, priceFragment, getString(R.string.price_page)).hide(priceFragment)
                    .add(R.id.fl_wrap, cultureFragment, getString(R.string.culture_page)).hide(cultureFragment)
                    .add(R.id.fl_wrap, homeFragment, getString(R.string.main_page)).show(homeFragment)
                    .commit();
        }
        rgTab.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        showFragment(checkedId);
    }

    public void showFragment(int resId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragments(ft);

        switch (resId) {
            case R.id.rb_home:
                Fragment homeFragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.main_page));
                if (homeFragment != null) {
                    ft.show(homeFragment);
                }
                break;
            case R.id.rb_culture:
                Fragment cultureFragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.culture_page));
                if (cultureFragment != null) {
                    ft.show(cultureFragment);
                }
                break;
            case R.id.rb_price:
                Fragment priceFragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.price_page));
                if (priceFragment != null) {
                    ft.show(priceFragment);
                }
                break;

            case R.id.rb_me:
                Fragment myFragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.me_page));
                if (myFragment != null) {
                    ft.show(myFragment);
                }
                break;
            default:
                break;
        }

        ft.commit();
    }

    // 当fragment已被实例化，就隐藏起来
    public void hideFragments(FragmentTransaction ft) {
        List<Fragment> mFragment = getSupportFragmentManager().getFragments();
        if (mFragment != null && mFragment.size() > 0) {
            for (Fragment fragment : mFragment) {
                ft.hide(fragment);
            }
        }
    }

}
